package com.nicefact.mailmonitor.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nicefact.mailmonitor.core.APP_CONSTANTS;
import com.nicefact.mailmonitor.entity.AppAdmin;
import com.nicefact.mailmonitor.entity.LogMessage;
import com.nicefact.mailmonitor.service.LogMessageService;

@Controller
public class LogMessageController {
	
	@Autowired private LogMessageService logMessageService;
	
	@RequestMapping("/getLogs.do")
	@ResponseBody
	public Map<String, Object> getLogs(HttpSession session){
		Map<String, Object> modalMap = new HashMap<String, Object>();
		AppAdmin appAdmin = (AppAdmin) session.getAttribute(APP_CONSTANTS.APP_ADMIN);
		List<LogMessage> logs = logMessageService.getLastTenLogs(appAdmin.getEmail());
		ArrayList<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		int id = 0;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm a");
		for(LogMessage log : logs){
			HashMap<String, Object> data = new HashMap<String, Object>();
			data.put("id", ++id);
			ArrayList<Object> row = new ArrayList<Object>();
			row.add(log.getMessage());
			row.add(formatter.format(log.getDate()));
			data.put("cell", row);
			rows.add(data);
		}
		modalMap.put("rows", rows);
		modalMap.put("page", 1);
		modalMap.put("total",
				10);

		modalMap.put("records", 10);
		return modalMap;
	}

}
